//
//  MineHeaderView.swift
//  KkmhSwift
//
//  Created by tenltrs on 2018/11/13.
//  Copyright © 2018 Tenltrs. All rights reserved.
//

import UIKit

class MineHeaderView: UIView {

    private var backView : UIImageView!

    private var iconView : UIImageView!
    
    private var nicknameView : UILabel!
    
    private var levelView : UIButton!
    
    private var signButton : UIButton!

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: 0)
        
        backgroundColor=UIColor.white
        
        initMain()
        
        initInfoView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initMain(){
        
        let image = UIImage(named: "ic_me_banner_backgroud_375x180_")!
        let ratio = image.size.width/image.size.height
        backView = UIImageView.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenWidth/ratio))
        backView.image=image
        self.addSubview(backView)
        
        let maskImage = UIImage(named: "found_projection_banner_375x10_")!
        let ratio2 = maskImage.size.width/maskImage.size.height
        
        let maskView = UIImageView.init(image: maskImage)
        backView.addSubview(maskView)
        maskView.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(maskView.snp.width).dividedBy(ratio2)
            make.left.bottom.equalToSuperview()
        }
        
        self.height=backView.frame.maxY+65
        
        
        let imageArr = ["ic_me_mall_22x22_","ic_me_item_wallet_22x22_"]
        let titleArr = ["积分市集","我的钱包"]
        let subTitleArr = ["好礼免费换","私房小金库"]
        
        var buttonArr = [UIView]()
        for (index,item) in imageArr.enumerated() {
            
            let view = UIView().then { (make) in
                
                let icon = UIImageView().then({ (make) in
                    make.image=UIImage(named: item)
                })
                make.addSubview(icon)
                icon.snp.makeConstraints({ (make) in
                    make.centerY.equalToSuperview()
                    make.left.equalToSuperview().offset(15)
                })
                
                let title = UIViewTool.mas_label(text: titleArr[index], textColor: UIColor.BLACK5TEXTCOLOR, textAligment: NSTextAlignment.left, fontSize: 15)
                make.addSubview(title)
                title.snp.makeConstraints({ (make) in
                    make.left.equalTo(icon.snp.right).offset(10)
                    make.top.equalToSuperview().offset(10)
                })
                
                let sub_title = UIViewTool.mas_label(text: subTitleArr[index], textColor: UIColor.BLACK9TEXTCOLOR, textAligment: NSTextAlignment.left, fontSize: 12)
                make.addSubview(sub_title)
                sub_title.snp.makeConstraints({ (make) in
                    make.left.equalTo(title)
                    make.top.equalTo(title.snp.bottom).offset(5)
                })
            }
            buttonArr.append(view)
            self.addSubview(view)
        }
        
        buttonArr.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.size.equalTo(CGSize(width: ScreenWidth/2, height: 65))
        }
        
        buttonArr.snp.distributeViewsAlong(axisType: NSLayoutConstraint.Axis.horizontal, fixedSpacing: 0)
        
        
        let line = UIViewTool.mas_view(backColor: UIColor.LineColor)
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: 1, height: 65/2))
            make.centerY.equalTo(backView.snp.bottom).offset(65/2)
         }
    }

    /// 初始化信息板
    func initInfoView() {
        
        iconView = UIViewTool.mas_imageView(backColor: nil, imageName: "ic_personal_avatar_83x83_").then({ (make) in
            make.layer.cornerRadius = 30
            make.layer.masksToBounds = true
        })
        backView.addSubview(iconView)
        iconView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 60, height: 60))
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(40)
        }
        
        nicknameView = UIViewTool.mas_label(text: "登录", textColor: UIColor.BLACK3TEXTCOLOR, textAligment: NSTextAlignment.left, fontSize: 15).then({ (make) in
            make.font=UIFont.boldSystemFont(ofSize: 16)
        })
        backView.addSubview(nicknameView)
        nicknameView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(iconView.snp.bottom).offset(10)
        }
        
        levelView = UIViewTool.mas_button(title: nil, image: "ic_members_my_nor_18x18_", backgroundImage: nil, titleColor: nil, fontSize: 0)
        backView.addSubview(levelView)
        levelView.snp.makeConstraints { (make) in
            make.centerY.equalTo(nicknameView)
            make.left.equalTo(nicknameView.snp.right).offset(5)
        }
        
        signButton = UIViewTool.mas_button(title: "签到领积分", image: nil, backgroundImage: nil, titleColor: UIColor(hexString: "#FFF59C"), fontSize: 12).then({ (make) in
            make.layer.backgroundColor=UIColor(hexString: "#FCA127")?.cgColor
            make.layer.cornerRadius=25/2
            make.layer.masksToBounds=true
        })
        backView.addSubview(signButton)
        signButton.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(nicknameView.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 25))
        }
    }
}
